package com.feeyin.radar.commons;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.support.PagedListHolder;

public class ListPage
{
  private static final Integer PAGESIZE = Integer.valueOf(20);

  public static List getPageList(List pageList, Integer pageSize, HttpServletRequest request) {
    int currentPage = 1;
    int flag = 1;
    List list = null;

    if (pageSize == null) {
      pageSize = PAGESIZE;
    }
    PagedListHolder newList = new PagedListHolder(pageList);
    newList.setPageSize(pageSize.intValue());

    if (request.getParameter("flag") == null) {
      currentPage = 1;
      int i = 1;
      while (true) {
        if (i == currentPage) {
          list = newList.getPageList();
          break;
        }
        newList.nextPage();
        i++;
      }
    }
    String inte = (String)request.getSession().getAttribute("currentPage");
    currentPage = Integer.parseInt(inte);
    flag = Integer.parseInt(request.getParameter("flag"));
    if (flag == 1) {
      currentPage = 1;
      int i = 1;
      while (true) {
        if (i == currentPage) {
          list = newList.getPageList();
          break;
        }
        newList.nextPage();
        i++;
      }
    }

    if (flag == 2) {
      if (currentPage > 1) {
        currentPage--;
        int i = 1;
        while (true) {
          if (i == currentPage) {
            list = newList.getPageList();
            break;
          }
          newList.nextPage();
          i++;
        }
      }
      currentPage = 1;
      int i = 1;
      while (true) {
        if (i == currentPage) {
          list = newList.getPageList();
          break;
        }
        newList.nextPage();
        i++;
      }

    }

    if (flag == 3) {
      if (currentPage < pageList.size() / newList.getPageSize() + 1) {
        currentPage++;
        int i = 1;
        while (true) {
          if (i == currentPage) {
            list = newList.getPageList();
            break;
          }
          newList.nextPage();
          i++;
        }
      }
      currentPage = pageList.size() / newList.getPageSize() + 1;
      int i = 1;
      while (true) {
        if (i == currentPage) {
          list = newList.getPageList();
          break;
        }
        newList.nextPage();
        i++;
      }

    }

    if (flag == 4) {
      currentPage = pageList.size() / newList.getPageSize() + 1;
      int i = 1;
      while (true) {
        if (i == currentPage) {
          list = newList.getPageList();
          break;
        }
        newList.nextPage();
        i++;
      }

    }

    request.setAttribute("countPage", Integer.valueOf(pageList.size() / newList.getPageSize() + 1));
    request.getSession().setAttribute("currentPage", Integer.toString(currentPage));
    return list;
  }
}